package com.livesource.client.web.sourceCode;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.livesource.client.model.entry.Method;
import com.livesource.client.model.entry.ProjectClass;
import com.livesource.client.utilities.FeatureNameGenerator;

/**
 * Show the structure and the documentation of the class in a specification
 * format. Enable Stakeholders understand the "business part" only of the source
 * code.
 * 
 * TODO: Show the component and the path at the top of the description.
 * 
 * @ToDo
 * @Feature
 */
public class FeatureDescription extends VerticalPanel {

	public static VerticalPanel vpComments = new VerticalPanel();

	public FeatureDescription(ProjectClass entry) {

		this.setSpacing(10);

		this.add(featureName(entry));

		this.add(featureDescription(entry));

		if (entry.getMethods() != null) {

			this.add(featureMethods(entry.getMethods()));
		}

		this.add(featureReferences(entry.getImports()));

		this.add(featureCreation(entry));

		vpComments.setSpacing(20);

		this.add(vpComments);
	}

	/**
	 * Show the name of the class with its package name and description.
	 */
	public static HTML featureName(ProjectClass entry) {

		String name = "<BR><BR><B><FONT SIZE=4>" + "&nbsp;&nbsp;"
				+ entry.getFeatureName() + "</FONT></B>";

		if (entry.getStatus() != null) {

			name += "&nbsp;&nbsp;&nbsp;<font color=blue>(Pending)</font>";
		}

		String pack = "<B>" + entry.getFeaturePath() + "</B>";

		HTML html = new HTML(pack + name);
		html.setWidth("400px");

		return html;
	}

	private static HorizontalPanel featureDescription(ProjectClass entry) {

		HorizontalPanel hp = new HorizontalPanel();

		hp.setSpacing(20);

		String text = null;
		if (entry.getFeatureDescription() != null
				&& !entry.getFeatureDescription().equals("")) {

			text = entry.getFeatureDescription();

		} else {

			text = " ";
		}

		hp.add(new HTML(text));

		return hp;
	}

	public static Grid featureMethods(final Method[] meths) {

		Grid table = new Grid(meths.length * 3, 2);

		table.getColumnFormatter().setWidth(0, "40");

		for (int i = 0; i < meths.length; i++) {

			Method meth = meths[i];

			table.setWidget((i * 3), 1, new HTML("<B>" + meth.getSpacedName()
					+ "</B>"));

			if (meth.getDescription() != null
					&& !meth.getDescription().equals("")) {

				table.setWidget((i * 3 + 1), 1, new HTML(meth.getDescription()));
			}

			table.setWidget((i * 3 + 2), 0, new Label(" "));
		}

		return table;
	}

	private static VerticalPanel featureReferences(String[] imports) {

		VerticalPanel vp = new VerticalPanel();

		VerticalPanel links = new VerticalPanel();

		boolean hasReference = false;

		if (imports != null) {

			for (final String link : imports) {

				final String domain = "";// ProjectInitialization.currentProject
				// .getDomain().replaceAll("/", ".");

				if (link.contains(domain)) {

					Anchor classeAnchor = new Anchor();
					// FeatureNameGenerator.getLastNameSpaced(link, "\\.",
					// locale));
					classeAnchor.addClickHandler(new ClickHandler() {
						public void onClick(ClickEvent sender) {

							String className = FeatureNameGenerator
									.getLastName(link, "\\.");

							String classPath = "";
							int classPathInit = domain.length() + 1;
							int classPathEnd = link.length()
									- className.length() - 1;
							if (classPathInit < classPathEnd) {

								classPath = link.substring(classPathInit,
										classPathEnd);
							}

							ProjectClass entry = new ProjectClass();
							entry.setFilePath(classPath.replaceAll("\\.", "/"));
							entry.setFileName(className + ".java");

							// new GetRemoteClass(entry,
							// OptionsIcons.OPTION_DESCRIPTION);
						}
					});

					links.add(classeAnchor);

					hasReference = true;
				}
			}
		}

		if (hasReference) {

			vp.add(new Label("See also:"));
		}

		vp.add(links);

		return vp;
	}

	private static VerticalPanel featureCreation(ProjectClass entry) {

		VerticalPanel vp = new VerticalPanel();

		String creation = "<br><br><font size=1>Created: "
				+ entry.getDateModified() + " &nbsp; by " + entry.getAuthor()
				+ "</font>";

		vp.add(new HTML(creation));

		return vp;
	}

}
